package com.jeff.android.lab.android.media;

import android.Manifest;
import android.media.MediaPlayer;
import android.os.Environment;
import android.widget.VideoView;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.permission.CheckPermission;
import com.jeff.permission.PermissionItem;
import com.jeff.permission.PermissionListener;

import java.io.File;

import butterknife.BindView;

/**
 * @author: duff
 * @date: 2019/3/15
 * @since: 1.0.0
 */
public class MediaPlayerFragment extends BaseFragment implements
        MediaPlayer.OnPreparedListener
        , MediaPlayer.OnBufferingUpdateListener
        , MediaPlayer.OnInfoListener
        , MediaPlayer.OnCompletionListener
        , MediaPlayer.OnErrorListener {

    @BindView(R.id.video_view)
    VideoView mVideoView;

    @Override
    protected int getRootLayout() {
        return R.layout.android_fragment_media_player;
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    protected void initData() {
        super.initData();

        play();
    }

    private void play() {
        CheckPermission.instance(mContext)
                .check(new PermissionItem(Manifest.permission.READ_EXTERNAL_STORAGE),
                        new PermissionListener() {
                            @Override
                            public void permissionGranted() {
                                String path = Environment.getExternalStorageDirectory().getAbsolutePath()
                                        + File.separator + "222.mp4";

                                if (new File(path).exists()) {
                                    mVideoView.setVideoPath(path);
                                    mVideoView.start();
                                }

                            }

                            @Override
                            public void permissionDenied() {

                            }
                        });

    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

    }

    @Override
    public void onCompletion(MediaPlayer mp) {

    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {

    }

}
